<template>
  <div  class='user'>
  <el-form ref="form"  class='content' :model="phone" :rules="from">
    <img src="../images/icon.png" width="274px">
    <el-form-item label="" prop="mobile">
      <el-input v-model="phone.mobile"  placeholder="请输入手机号"></el-input>
    </el-form-item>
    <el-form-item label="" prop="code">
      <el-input v-model="phone.code" placeholder="请输入验证码"></el-input>
    </el-form-item>
    <el-form-item label="" prop="checked">
      <el-checkbox v-model="phone.checked">已阅读并同意用户协议和隐私条款</el-checkbox>
    </el-form-item>
    <el-form-item>
      <el-button  class="user-btn" type="primary" @click="onSubmit" :loading="loading">立即创建</el-button>
    </el-form-item>
  </el-form></div>
</template>

<script>
import { login } from '../utils/request'
export default {
  name: 'index',

  data () {
    return {
      phone: {
        mobile: '',
        code: '',
        checked: false
      },
      loading: false,
      from: {
        mobile: [
          // 设置能不为空  trigger: 'blur' 失去焦点时验证 trigger: 'change' 数据改变时验证
          { required: true, message: '手机号不可以为空', trigger: 'blur' },
          // 设置正则表达式
          { pattern: /^1[3456789]\d{9}$/, message: '请输入正确手机号', trigger: 'blur' }
        ],
        code: [
          //
          { required: true, message: '验证码不可以为空', trigger: 'blur' },
          { pattern: /^\d{4,5}$/, message: '验证码格式不正确', trigger: 'blur' }
        ],
        checked: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error('请先同意用户协议'))
              }
              callback()
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      // 数据提交时再次验证一次
      this.$refs['form'].validate(a => {
        // console.log(a)
        if (!a) {
          return null
        } else {
          this.login()
        }
      })
    },
    login () {
      // 判断是否阅读用户协议(已经使用element-ui的自定义校验规则了)
      // if (!this.checked) {
      //   this.$message({
      //     message: '请先已阅读并同意用户协议和隐私条款',
      //     type: 'warning'
      //   })
      //   return
      // }
      this.loading = true
      login(this.phone).then(res => {
        console.log(res)
        this.$message({
          message: '登录成功',
          type: 'success'
        })
        this.loading = false
      }).catch(err => {
        this.$message.error('验证码错误')
        console.log(err)
        this.loading = false
      })
    }
  }

}
</script>
<style lang="less">
  .user{
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
background: url("../images/aaa.jpg") no-repeat;
    height: 100%;
    background-size: cover;
    .content{
      border-radius: 5px;
      background-color: white;
      padding: 100px;
      min-width: 300px;
      .user-btn{
        width: 100%;
      }
    }
    img{
      padding-bottom: 20px;
    }
  }
</style>
